library(tidyverse)
library(kableExtra)



# Table 1 -----------------------------------------------------------------


all <- read_rds("Data/Table1.RDS")

### create table dataframes

t1 <- all %>% 
  ungroup %>% 
  filter(!is.na(donation_type)) %>% 
  filter(donation_type %in% c("man to woman", "woman to woman")) %>% 
  group_by(donation_type) %>% 
  summarize(total_recip = sum(total_giving)) %>% 
  ungroup %>% 
  mutate(total = sum(total_recip), prop = total_recip/total) %>% 
  select(donation_type, prop) %>% 
  mutate(donation_type = str_to_title(donation_type)) %>% 
  mutate(category = "")

t1.party <- all %>% 
  ungroup %>% 
  filter(!is.na(donation_type) & !is.na(party_type)) %>% 
  filter(donation_type %in% c("man to woman", "woman to woman")) %>% 
  group_by(donation_type, party_type) %>% 
  summarize(total_recip = sum(total_giving)) %>% 
  ungroup %>% 
  group_by(party_type) %>% 
  mutate(total = sum(total_recip), prop = total_recip/total) %>% 
  select(donation_type, party_type, prop) %>% 
  mutate(donation_type = str_to_title(donation_type)) %>% 
  mutate(category = "")

t3 <- all %>% 
  ungroup %>% 
  filter(!is.na(donation_type) & !is.na(early2)) %>% 
  filter(donation_type %in% c("man to woman", "woman to woman")) %>% 
  group_by(donation_type, early2) %>% 
  summarize(total_recip = sum(total_giving)) %>% 
  ungroup %>% 
  group_by(early2) %>% 
  mutate(total = sum(total_recip), prop = total_recip/total) %>% 
  arrange(early2) %>% 
  mutate(early2 = ifelse(early2 == 0, "Late", "Early")) %>% 
  ungroup %>% 
  select(donation_type, category = early2, prop)

t3.party <- all %>% 
  ungroup %>% 
  filter(!is.na(donation_type) & !is.na(early2) & !is.na(party_type)) %>% 
  filter(donation_type %in% c("man to woman", "woman to woman")) %>% 
  group_by(donation_type, party_type, early2) %>% 
  summarize(total_recip = sum(total_giving)) %>% 
  ungroup %>% 
  group_by(party_type, early2) %>% 
  mutate(total = sum(total_recip), prop = total_recip/total) %>% 
  arrange(early2) %>% 
  mutate(early2 = ifelse(early2 == 0, "Late", "Early")) %>% 
  ungroup %>% 
  select(donation_type, party_type, category = early2, prop)


## pool together open seats and challengers, compare to incumb
t5.1 <- all %>% 
  ungroup %>% 
  filter(!is.na(donation_type) & !is.na(incum_chall)) %>% 
  filter(donation_type %in% c("man to woman", "woman to woman")) %>% 
  mutate(incumb = ifelse(incum_chall == "I", 1, 0)) %>% 
  group_by(donation_type, incumb) %>% 
  summarize(total_recip = sum(total_giving)) %>% 
  ungroup %>% 
  group_by(incumb) %>% 
  mutate(total = sum(total_recip), prop = total_recip/total) %>% 
  arrange(incumb) %>% 
  mutate(incumb = case_when(
    incumb == 0 ~ "Challenger/Open",
    incumb == 1 ~ "Incumbent"
  )) %>% 
  ungroup %>% 
  select(donation_type, category = incumb, prop)


## pool together open seats and challengers, compare to incumb
t5.1.party <- all %>% 
  ungroup %>% 
  filter(!is.na(donation_type) & !is.na(incum_chall) & !is.na(party_type)) %>% 
  filter(donation_type %in% c("man to woman", "woman to woman")) %>% 
  mutate(incumb = ifelse(incum_chall == "I", 1, 0)) %>% 
  group_by(donation_type, party_type, incumb) %>% 
  summarize(total_recip = sum(total_giving)) %>% 
  ungroup %>% 
  group_by(incumb, party_type) %>% 
  mutate(total = sum(total_recip), prop = total_recip/total) %>% 
  arrange(incumb) %>% 
  mutate(incumb = case_when(
    incumb == 0 ~ "Challenger/Open",
    incumb == 1 ~ "Incumbent"
  )) %>% 
  ungroup %>% 
  select(donation_type, party_type, category = incumb, prop)


t7 <- all %>% 
  ungroup %>% 
  filter(!is.na(donation_type)) %>% 
  filter(donation_type %in% c("man to woman", "woman to woman")) %>% 
  filter(quartile == 1 | quartile == 4) %>% 
  group_by(donation_type, quartile) %>% 
  summarize(total_recip = sum(total_giving)) %>% 
  ungroup %>% 
  group_by(quartile) %>% 
  mutate(total = sum(total_recip), prop = total_recip/total) %>% 
  arrange(quartile) %>% 
  mutate(quartile = ifelse(quartile == 1, "Low-Funded", "High-Funded")) %>% 
  ungroup %>% 
  select(donation_type, category=quartile, prop)

t7.party <- all %>% 
  ungroup %>% 
  filter(!is.na(donation_type) & !is.na(party_type)) %>% 
  filter(donation_type %in% c("man to woman", "woman to woman")) %>% 
  filter(quartile == 1 | quartile == 4) %>% 
  group_by(donation_type, party_type, quartile) %>% 
  summarize(total_recip = sum(total_giving)) %>% 
  ungroup %>% 
  group_by(quartile, party_type) %>% 
  mutate(total = sum(total_recip), prop = total_recip/total) %>% 
  arrange(quartile) %>% 
  mutate(quartile = ifelse(quartile == 1, "Low-Funded", "High-Funded")) %>% 
  ungroup %>% 
  select(donation_type, party_type, category=quartile, prop)


t.gender <- bind_rows(t1, t3, t5.1, t7)
t.party <- bind_rows(t1.party, t3.party, t5.1.party, t7.party)


t.final <- t.gender %>% left_join(t.party %>% 
                                    filter(party_type == "Rep. to Rep.") %>% 
                                    rename(prop_rep = prop) %>% 
                                    ungroup %>% 
                                    select(-party_type)) %>% 
  left_join(t.party %>% 
              filter(party_type == "Dem. to Dem.") %>% 
              rename(prop_dem = prop) %>% 
              ungroup %>% 
              select(-party_type))



t.final %>% 
  mutate(prop = round(prop, 2),
         prop_dem = round(prop_dem, 2),
         prop_rep = round(prop_rep, 2)) %>% 
  mutate(donation_type = str_to_lower(donation_type),
         donation_type = ifelse(donation_type == "man to woman", "Man to Woman", "Woman to Woman")) %>% 
  select(-category) %>% 
  kbl(
    col.names = c(" ", "All", "Rep.", "Dem."), 
    booktabs = T,
    caption = "Gender--Based Giving by Recipient Type",    "latex"
  ) %>% 
  kable_styling(font_size = 10) %>% 
  column_spec(c(1), width = "3.5cm") %>% 
  row_spec(1:14, font_size = 8) %>% 
  pack_rows("All Giving", 1, 2) %>% 
  pack_rows("Late Giving", 3, 4, latex_gap_space = "2mm") %>% 
  pack_rows("Early Giving", 5, 6, latex_gap_space = "2mm") %>% 
  pack_rows("Challenger/Open Seat", 7, 8, latex_gap_space = "2mm") %>% 
  pack_rows("Incumbent", 9, 10, latex_gap_space = "2mm") %>% 
  pack_rows("Low-Funded Recip.", 11, 12, latex_gap_space = "2mm") %>% 
  pack_rows("High-Funded Recip.", 13, 14, latex_gap_space = "2mm") %>% 
  add_header_above(c("Proportion of Giving" = 2:4)) 



# Figure 2 and 3, Table 9 ----------------------------------------------------------

lpacs <- read_rds("Data/Figure2_Figure3_Table9_Table10.RDS")

lpacs %>% 
  ungroup %>% 
  group_by(cycle, chamber) %>% 
  distinct(cmteid_bioguideid) %>% 
  count() %>% 
  mutate(n = ifelse(chamber == "House", n/435, n/100)) %>% 
  ggplot(aes(x = cycle, y = n, group = chamber, linetype = chamber)) +
  geom_line() +
  scale_color_grey() +
  xlab("Election Cycle") +
  ylab("Proportion of Chamber with LPAC") +
  theme_bw()

ggsave("Figure2.pdf", units = "in", width = 6, height = 4)


lpacs %>% 
  ungroup %>% 
  group_by(cycle, chamber) %>% 
  summarize(total = sum(total)/1000000) %>% 
  ggplot(aes(x = cycle, y = total, group = chamber, linetype = chamber)) +
  geom_line() +
  scale_color_grey() +
  xlab("Election Cycle") +
  ylab("Amount Given (in millions)") +
  theme_bw()

ggsave("Figure3.pdf", units = "in", width = 6, height = 4)


## Table 9
lpacs %>% 
  ungroup %>% 
  filter(chamber == "House") %>% 
  group_by(cycle, cmteid_bioguideid) %>% 
  summarize(mean = mean(total), n = n()) %>% 
  ungroup %>% 
  group_by(cycle) %>% 
  summarize(mean_giving = round(mean(mean)), sd_giving = round(sd(mean)), n_mean = round(mean(n)), n_sd = round(sd(n))) %>% 
  kbl(
    col.names = c("Cycle","Amount (Mean)", "Amount (SD)", "Num. Donations (Mean)", "Num. Donations (SD)"), 
    booktabs = T, linesep = "",
    caption = "Descriptive Statistics of Per-Member LPAC Giving: House",    "latex"
  ) %>% 
  kable_styling(font_size = 10) 


## Table 10
lpacs %>% 
  ungroup %>% 
  filter(chamber == "Senate") %>% 
  group_by(cycle, cmteid_bioguideid) %>% 
  summarize(mean = mean(total), n = n()) %>% 
  ungroup %>% 
  group_by(cycle) %>% 
  summarize(mean_giving = round(mean(mean)), sd_giving = round(sd(mean)), n_mean = round(mean(n)), n_sd = round(sd(n))) %>% 
  kbl(
    col.names = c("Cycle","Amount (Mean)", "Amount (SD)", "Num. Donations (Mean)", "Num. Donations (SD)"), 
    booktabs = T, linesep = "",
    caption = "Descriptive Statistics of Per-Member LPAC Giving: Senate",    "latex"
  ) %>% 
  kable_styling(font_size = 10) 
